﻿Public Class FormPicking
    Public usr As String
    Private WithEvents UcDigitalKeyboard1 As ucDigitalKeyboard
    Public ty As String
    Dim dt As DataTable
    Dim objGjbMst As New clsGjbMst
    Dim objFlbMst As New clsFlbMst
    Dim objPWck As New clsPWck
    Dim objAutoNumber As New clsAutoNumber
    Private Sub FormPicking_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        initdt()
        setDgvData()
    End Sub
    Private Sub initdt()
        dt = New DataTable
        dt.Columns.Add("batch_no")
        dt.Columns.Add("out_num", Type.GetType("System.Decimal"))
        dt.Columns.Add("oper_type")
        dt.Columns.Add("type_value")
        dt.Columns.Add("material_code")
        dt.Columns.Add("assist_marker")
        dgv.AutoGenerateColumns = False
        dgv.DataSource = dt
    End Sub
    Private Sub setDgvData()
        Dim dtPwCk As DataTable = objPWck.getPwCkbyTv(ty)
        dtPwCk.Columns.Add("oper_type_name")
        If dtPwCk.Rows.Count > 0 Then
            For Each row As DataRow In dtPwCk.Rows
                If row("oper_type") = "0" Then
                    row("oper_type_name") = "骨架板"
                Else
                    row("oper_type_name") = "法兰板"
                End If
            Next
        End If
        dgv1.AutoGenerateColumns = False
        dgv1.DataSource = dtPwCk
    End Sub

    Private Sub dgv1_SelectionChanged(sender As Object, e As EventArgs) Handles dgv1.SelectionChanged
        If dgv1.SelectedRows.Count > 0 Then
            'Dim dv As String = dgv1.SelectedRows(0).Cells("Col_type_value").Value.ToString()
            'If ty.Equals("0") Then
            '    Dim dtgjbmst As DataTable = objGjbMst.selectAll(dv)
            '    txtCk_all_count.Value = Decimal.Parse(dtgjbmst.Rows(0)("cur_num").ToString)
            'Else
            '    Dim dtflbmst As DataTable = objFlbMst.selectAll(dv)
            '    txtCk_all_count.Value = Decimal.Parse(dtflbmst.Rows(0)("cur_num").ToString)
            'End If
            txt_chk_num.Value = Decimal.Parse(dgv1.SelectedRows(0).Cells("Col_cur_num").Value.ToString())
        Else
            txt_chk_num.Value = 0
        End If
    End Sub

    Private Sub btn_Add_Click(sender As Object, e As EventArgs) Handles btn_Add.Click
        If dgv1.SelectedRows.Count > 0 Then
            Dim dr As DataRow() = dt.Select("oper_type = '" & dgv1.SelectedRows(0).Cells("Col_oper_type_name").Value.ToString() & "' and type_value = '" & dgv1.SelectedRows(0).Cells("Col_type_value").Value.ToString() & "'")
            Dim drc As Decimal = 0
            For index = 0 To dr.Length - 1
                drc = drc + Decimal.Parse(dr(index)("out_num").ToString())
            Next
            If txt_chk_num.Value > 0 Then
                If Decimal.Parse(dgv1.SelectedRows(0).Cells("Col_cur_num").Value.ToString()) - Decimal.Parse(drc.ToString()) >= txt_chk_num.Value Then
                    dt.Rows.Add(dgv1.SelectedRows(0).Cells("ColBatchNo").Value.ToString(),
                                txt_chk_num.Value,
                                dgv1.SelectedRows(0).Cells("Col_oper_type_name").Value.ToString(),
                                dgv1.SelectedRows(0).Cells("Col_type_value").Value.ToString(),
                                dgv1.SelectedRows(0).Cells("Col_material_code").Value.ToString(),
                                dgv1.SelectedRows(0).Cells("Col_assist_marker").Value.ToString())
                Else
                    MsgBox("出库数量 >= (当前选中的库存批次数量 - 已进行领料的数量)！")
                End If
            Else
                MsgBox("领料数量需大于0")
            End If

        End If
    End Sub

    Private Sub txt_chk_num_MouseDown(sender As Object, e As MouseEventArgs) Handles txt_chk_num.MouseDown
        UcDigitalKeyboard1 = New ucDigitalKeyboard
        If sender.MaskBox.Mask.EditMask = "d" Then
            UcDigitalKeyboard1.B_01 = False
        Else
            UcDigitalKeyboard1.B_01 = True
        End If

        If sender.GetType().Name.Equals("TextEdit") Then
            UcDigitalKeyboard1.B_02 = True
        Else
            UcDigitalKeyboard1.B_02 = False
        End If

        UcDigitalKeyboard1.Txt = sender

        Me.Controls.Add(UcDigitalKeyboard1)
        UcDigitalKeyboard1.Show()
        UcDigitalKeyboard1.BringToFront()
        UcDigitalKeyboard1.Focus()
    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        If dgv.Rows.Count > 0 Then
            If MsgBox("您确定要进行删除吗？", MsgBoxStyle.OkCancel, "提示") = MsgBoxResult.Ok Then
                dt.Rows.RemoveAt(dgv.SelectedRows(0).Index)
            End If
        End If
    End Sub
    Private Function toNewEn(batch_no As String,
                             oper_type As String,
                             type_value As String,
                             material_code As String,
                             out_num As String,
                             assist_marker As String) As entitypw_ck
        Dim en As New entitypw_ck
        en.batch_no = batch_no
        If oper_type = "法兰板" Then
            en.oper_type = "1"
        Else
            en.oper_type = "0"
        End If
        en.type_value = type_value
        en.material_code = material_code
        en.out_num = Decimal.Parse(out_num)
        en.location = String.Empty
        en.pt_pgd_code = String.Empty
        en.validity = "1"
        en.assist_marker = assist_marker
        Return en
    End Function
    Private Sub btnCK_Click(sender As Object, e As EventArgs) Handles btnCK.Click
        If dt.Rows.Count > 0 Then
            If MsgBox("您确定要进行领料吗？", MsgBoxStyle.OkCancel, "提示") = MsgBoxResult.Ok Then
                Dim code As String = Now.ToString("yyyyMMdd") & "-" & objAutoNumber.getNextKeyNo("pw_ck_code", "pwckAutoNumber")
                For i = 0 To dt.Rows.Count - 1
                    Dim en As entitypw_ck = toNewEn(
                        dt.Rows(i)("batch_no").ToString,
                        dt.Rows(i)("oper_type").ToString,
                        dt.Rows(i)("type_value").ToString,
                        dt.Rows(i)("material_code").ToString,
                        dt.Rows(i)("out_num").ToString,
                        dt.Rows(i)("assist_marker").ToString())
                    objPWck.insertCk(code, en, usr)
                Next
                objPWck.insertCk(code, usr)
                objAutoNumber.setNextKeyNo("pw_ck_code", "pwckAutoNumber")
                initdt()
                setDgvData()
                MsgBox("领料成功")
            End If
        End If
    End Sub
End Class

Public Class entitypw_ck
    Public batch_no As String   ' 批次
    Public location As String   ' 库位号
    Public pt_pgd_code As String   ' 派工单号
    Public out_num As Decimal   ' 出库数量
    Public cur_num As String   ' 当前数量
    Public update_time As String   ' 修改时间
    Public oper_type As String
    Public type_value As String
    Public material_code As String  ' 物料编码
    Public validity As String
    Public assist_marker As String
End Class